package org.pz.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import java.util.Objects;

@Slf4j
@Configuration
public class RateLimiterConfig {

    /**
     * 按照客户端 ip 限流
     * */
    @Bean
    public KeyResolver requestPathResolver() {
        return exchange -> {
            log.info( "当前请求路径：{}", exchange.getRequest().getPath() );
            return Mono.just(
                    Objects.requireNonNull(exchange.getRequest()
                                    .getRemoteAddress())
                            .getAddress()
                            .getHostAddress()
            );
        };
    }

}
